package com.huangfu.learning.javabase.java.collections.set;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/7/21
 * Time: 15:28
 * Version: 1.0
 * Description:HashSet测试
 **/
public class HashSetTest {
    public static void main(String[] args) {
        Set hs = new HashSet();
        hs.add(new Cat("henrry", 1, "white"));
        hs.add(new Cat("hali", 2, "balck"));
        hs.add(new Cat("ami", 3, "yellow"));
        System.out.println("----------HashSet结果集--------------------------");
        System.out.println(hs);
        //取出集合中的每个元素
        System.out.println("----------遍历hashset--------------------------");

        Iterator iterator = hs.iterator();
        while (iterator.hasNext()) {
            Cat cat = (Cat) iterator.next();
            System.out.println(cat);
        }
    }
}

class Cat implements Serializable {

    public Cat(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    private String name;
    private int age;
    private String color;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}
